package com.tutucms.service.admin.service;

import com.alibaba.fastjson.TypeReference;
import com.tutucms.common.exception.InvokException;
import com.tutucms.common.exception.NotFondException;
import com.tutucms.common.model.response.Paging;
import com.tutucms.common.util.BeanFiller;
import com.tutucms.common.util.JsonUtil;
import com.tutucms.repo.entity.Article;
import com.tutucms.repo.entity.Catalog;
import com.tutucms.repo.mapper.ArticleMapper;
import com.tutucms.repo.mapper.CatalogMapper;
import com.tutucms.repo.util.QueryHelper;
import com.tutucms.service.admin.model.ro.AddArticleRo;
import com.tutucms.service.admin.model.ro.ArticleRo;
import com.tutucms.service.admin.model.ro.EditArticleRo;
import com.tutucms.service.model.vo.content.ArticleFillVo;
import com.tutucms.service.model.vo.content.ArticleVo;
import com.tutucms.service.model.vo.content.CatalogVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by wangxudong on 2020/10/26.
 *
 * @version: 1.0
 * @modified :
 */
@Service
@Slf4j
public class ContentService {

    @Autowired
    private CatalogMapper catalogMapper;

    @Autowired
    private ArticleMapper articleMapper;

    public List<CatalogVo> getRootCatalog() {

        return BeanFiller.target(CatalogVo.class).acceptList(catalogMapper.getRootCatalog());
    }

    public List<CatalogVo> getSubCatalog(String parentCode) {

        Catalog catalog = catalogMapper.getCatalogByCode(parentCode);

        if (catalog == null) {
            throw new NotFondException("父目录未找到");
        }

        return BeanFiller.target(CatalogVo.class).acceptList(catalogMapper.getSubCatalog(catalog.getId()));
    }

    public CatalogVo getCatalogVo(String code) {

        return BeanFiller.target(CatalogVo.class).accept(catalogMapper.getCatalogByCode(code));
    }

    public Paging<ArticleVo> getArticleListByCode(ArticleRo articleRo) {
        Catalog catalog = catalogMapper.getCatalogByCode(articleRo.getCatalogCode());

        if (catalog == null) {
            throw new NotFondException("父目录未找到");
        }

        QueryHelper.setupPageCondition(articleRo);

        return QueryHelper.getPaging(articleMapper.getArticleByCatalogId(catalog.getId()), ArticleVo.class, (articleVo, article) -> {
            articleVo.setImageList(JsonUtil.fromJson(article.getImages(), new TypeReference<List<String>>() {
            }));
        });
    }

    public ArticleFillVo getArticleVo(Long id) {

        return BeanFiller.target(ArticleFillVo.class).acceptDefault(articleMapper.selectByPrimaryKey(id),
                (articleVo, article) -> articleVo.setImageList(JsonUtil.fromJson(article.getImages(),
                        new TypeReference<List<String>>() {
                        })));
    }

    public void deleteArticle(String catalogCode, Long id) {

        articleMapper.deleteByPrimaryKey(id);
    }

    public void addArticle(AddArticleRo ro) {
        Catalog catalog = catalogMapper.getCatalogByCode(ro.getCatalogCode());
        Article article = BeanFiller.target(Article.class).acceptDefault(ro, (a, add) -> {
            a.setImages(JsonUtil.toJson(add.getImagesList()));
            a.setCatalogId(catalog.getId());
            a.setSort(System.currentTimeMillis());
            a.setCreateTime(new Date());
        });

        articleMapper.insertSelective(article);
    }

    public void updateArticle(EditArticleRo ro) {

        Catalog catalog = catalogMapper.getCatalogByCode(ro.getCatalogCode());
        Article article = BeanFiller.target(Article.class).acceptDefault(ro, (a, add) -> {
            a.setImages(JsonUtil.toJson(add.getImagesList()));
            a.setCatalogId(catalog.getId());
        });
        articleMapper.updateByPrimaryKeySelective(article);
    }

    public void topArticle(Long id) {

        Article article = articleMapper.selectByPrimaryKey(id);
        if (article != null) {
            article.setIsTop(!article.getIsTop());
            articleMapper.updateByPrimaryKeySelective(article);
        }

    }

    public void sortArticle(Long id, String action) {

        Article article = articleMapper.selectByPrimaryKey(id);
        if (article == null) {

            throw new InvokException("文章不存在");
        }
        if (action.equals("up")) {
            Article up = articleMapper.getBeforeSortArticle(article.getSort());

            article.setSort(up.getSort() + 1);
        } else {
            Article down = articleMapper.getNextSortArticle(article.getSort());

            article.setSort(down.getSort() - 1);
        }

        articleMapper.updateByPrimaryKeySelective(article);
    }
}
